$(function(){
	var base=sessionStorage.getItem("base"); 
	$('#back').on('click',function(){
		history.back();
	});
	var accounts=sessionStorage.getItem('correlationaccount'),
	selectedAccounts=[],
//	accountHtml='<div class="account_list_ul"><div class="account_list_li"><a appkey="appKey" custaccount="cust_account"><div class="account_content"><h2>name</h2><p>注册来源：<span style="margin-right:15px">appName</span>账户余额：<span>account_balance元 </span></p></div></a><i><img src="images/tipss.png" width="19" height="19"></i></div>$rscontent</div>',
	accountHtml='<div class="account_list_ul"><div class="account_list_li"><a><b appkey="appKey" custaccount="cust_account"></b><div class="account_content"><h2>name</h2> <p>账户余额：<span>account_balance </span></p></div></a><i></i></div>$rscontent</div>',	
	accountResourceHtml='<div class="account_table"><table><tr><th scope="col">应用名称</th><th scope="col">产品名称</th><th scope="col">资源可用量</th><th scope="col">类型</th></tr>$content</table></div>';	   
	if(accounts){
		accounts=JSON.parse(accounts);
		var htmls=[];
		for (var i = 0; i < accounts.length; i++) {
			var ah=accountHtml.replace(/name|appName|account_balance|appKey|cust_account/g,function(value){
				return accounts[i][value]||value;
			}),rsh='';
			var resources=accounts[i].resources,html=[];
			if(resources&&resources.length>0){
				for (var m = 0; m < resources.length; m++) {
					html.push('<tr><td>',resources[m].appName,'</td>');
					html.push('<td>',resources[m].product_name,'</td>');
					html.push('<td>',resources[m].balance,'</td>');
					html.push('<td>',resources[m].resource_type,'</td></tr>');
				}
				rsh=accountResourceHtml.replace('$content',html.join(''));
			}
			htmls.push(ah.replace('$rscontent',rsh));
		}
		$('#content').append(htmls.join(''));
	}
//	  $(document.body).on('click','.account_list_ul .account_list_li i',function(){
//		  var oTable=$(this).parent('.account_list_li').siblings('.account_table');	
//		  var oI=$(this);
//		  if(oTable.css('display')=='none'){
//			  oI.css('opacity','0.6');
//			  $(this).parent('.account_list_li').siblings('.account_table').slideToggle(500); 
//		  }else if(oTable.css('display')=='block'){
//			  oI.css('opacity','1');
//			  $(this).parent('.account_list_li').siblings('.account_table').slideToggle(500); 
//		  }		  
//	  });
	
	  $(document.body).on('click','.account_list_ul .account_list_li',function(){
		  var oTable=$(this).siblings('.account_table');	
		  var oI=$(this).find('i');	  
		  if(oTable.css('display')=='none'){
			  oI.addClass('active');
			  if(utils.isIeClient(8))
			     oI.css({'background':'url(images/right_2.png) no-repeat center 1px','width':'19px','height':'11px'} ); 
			$(this).siblings('.account_table').slideToggle(500); 
			  }else if(oTable.css('display')=='block'){
				  oI.removeClass('active');
				  if(utils.isIeClient(8))
			     oI.css({'background':'url(images/right.png) no-repeat center 1px','width':'11px','height':'19px'} ); 
			$(this).siblings('.account_table').slideToggle(500); 
				  }			  
	  });
	  
	  $(document.body).on('click','.account_list_li b',function(event){
		  event.stopPropagation();
		  var ac=$(this);
		  if(ac.hasClass('checked')){
			  ac.removeClass('checked');
			  for (var k = 0; k < selectedAccounts.length; k++) {
				if(selectedAccounts[k]===ac.attr('custaccount')){
					selectedAccounts.splice(k,1);
					break;
				}
			  }
		  }else{
			  ac.addClass('checked');
			  selectedAccounts.push(ac.attr('custaccount'));
		  }
	  });
	  	  
	  $('#chooseMain').on('click',function(){
		  if(selectedAccounts.length===0){
	    	new UserPrompt('请选择需要合并的账户');
	        return;
		  }
		  if(selectedAccounts.length===1){
			  new UserPrompt('至少选择两个账户进行合并操作,请重新选择');
			  return;
		  }
		  location.assign('choose_main.html?data='+JSON.stringify(selectedAccounts));
	  });
	  var paramJSON=JSON.parse(sessionStorage.getItem('parameter'));
	  var showHeader=paramJSON.showHeader||'1';//不传默认展示标题栏
	  if(showHeader==='1'){  
		  $('#header').css('display','block'); 
		  $('#content').css('margin-top','43px'); 
	  }
	  $('#registerAccount').on('click',function(){//普通注册接口
	    $.ajax({
	      type : "get",
          dataType : "jsonp",
	      url : base+"orgacc/orgAccReg.do",
	      data : paramJSON,
	      success:function(data){
	    	  if(data.result==='0'){
	  	    	new UserPrompt('账户注册成功');
                location.assign('success.html?message=账户注册成功'); 
	    	  }	    	  
	      },
	      error:function(data){
	    	  if(data&&data.describe)
		        new UserPrompt(data.describe);
	      }
		 });
	  });
	  
	  $('#content').css({
		  'height':utils.getHeight([$('#header'),$('#chooseMain'),$('#registerAccount')]),
		  'overflow-y':'auto'
	  });
});